﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Text.RegularExpressions;
namespace Common
{
    public class Input
    {
        

        /// <summary>
        /// 从html中提取纯文本
        /// </summary>
        /// <param name="strHtml"></param>
        /// <returns></returns>
        public static string StripHT(string strHtml)  
        {
            Regex regex = new Regex("<.+?>", RegexOptions.IgnoreCase);
            string strOutput = regex.Replace(strHtml, "");//替换掉"<"和">"之间的内容
            strOutput = strOutput.Replace("<", "");
            strOutput = strOutput.Replace(">", "");
            strOutput = strOutput.Replace("&nbsp;", "").Replace("&rdquo;", "“").Replace("&ldquo;", "“").Trim();
            return strOutput;
        }

        /// <summary>   
        /// 传入URL返回网页的html代码   
        /// </summary>   
        /// <param name="Url">URL</param>   
        /// <returns></returns>   
        public static string getUrltoHtml(string Url)
        {
            string errorMsg = "";
            try
            {
                System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
                // Get the response instance.   
                System.Net.WebResponse wResp = wReq.GetResponse();
                // Read an HTTP-specific property   
                //if (wResp.GetType() ==HttpWebResponse)   
                //{   
                //DateTime updated =((System.Net.HttpWebResponse)wResp).LastModified;   
                //}   
                // Get the response stream.   
                System.IO.Stream respStream = wResp.GetResponseStream();
                // Dim reader As StreamReader = New StreamReader(respStream)   
                System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("UTF-8"));
                return reader.ReadToEnd();

            }
            catch (System.Exception ex)
            {
                errorMsg = ex.Message;
            }
            return "";
        }

       

        /// <summary>
        /// 过滤危险html
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string WipeScript(string html)
        {
            Regex regex1 = new Regex(@"<script[\s\s]+</script *>",RegexOptions.IgnoreCase);
            Regex regex2 = new Regex(@" href *= *[\s\s]*script *:", RegexOptions.IgnoreCase);
            Regex regex3 = new Regex(@" on[\s\s]*=", RegexOptions.IgnoreCase);
            Regex regex4 = new Regex(@"<iframe[\s\s]+</iframe *>", RegexOptions.IgnoreCase);
            Regex regex5 = new Regex(@"<frameset[\s\s]+</frameset *>", RegexOptions.IgnoreCase);
            html = regex1.Replace(html, ""); //过滤<script></script>标记 
            html = regex2.Replace(html, ""); //过滤href=javascript: (<a>) 属性 
            html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件 
            html = regex4.Replace(html, ""); //过滤iframe 
            html = regex5.Replace(html, ""); //过滤frameset 
            return html;
        }

        

        /// <summary>
        /// 获取6个验证取机数
        /// </summary>
        /// <returns></returns>
        public static string ValidateCode(int len)
        {
            StringBuilder sb = new StringBuilder();
            Random rd = new Random();
            for (int i = 1; i <= len; i++)
            {
                sb.Append(rd.Next(9).ToString());
            }
            return sb.ToString();
        }
    }
}
